<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestAlerter extends PHPUnit_Framework_TestCase {

	public function test_add_alert_with_message_only() {
		$alerter  = new Alerter();

		$message = "This is my message to the world.";
		$link = "And this is the link";
		$alerter->add_alert($message, $link);

		$actual = $alerter->get_alerts();
		$this->assertNotEquals(null, $actual);
		$this->assertEquals($message, $actual[0]['message']);
		$this->assertEquals($link, $actual[0]['link']);
		$this->assertEquals('', $actual[0]['delete']);
	}

	public function test_add_alert_with_delete() {
		$alerter = new Alerter();

		$message = "This is my message to the world.";
		$link = "And this is the link";
		$delete = "Delete stuff";
		$alerter->add_alert($message, $link, false, $delete);

		$actual = $alerter->get_alerts();
		$this->assertNotEquals(null, $actual);
		$this->assertEquals($message, $actual[0]['message']);
		$this->assertEquals($link, $actual[0]['link']);
		$this->assertEquals($delete, $actual[0]['delete']);

		$alerter->add_alert($message, $link, false, $delete);

		$actual = $alerter->get_alerts_delete();
		$this->assertEquals("$delete,$delete", $actual); 
	}

	public function test_add_alert_with_target() {
		$alerter = new Alerter();

		$message = "This is my message to the world.";
		$link = "And this is the link";

		$db_access = $this->getMock('DbAccessor');

		$db_access->expects($this->once())
				  ->method('db_query_insert')
				  ->will($this->returnValue('the query'));

		$db_access->expects($this->once())
				  ->method('db_sqlize')
				  ->with($message);

		$db_access->expects($this->once())
				  ->method('db_do_query')
				  ->with('the query');
		

		$alerter->set_db_accessor($db_access);
		$alerter->add_alert($message, $link, true);
	}

	function test_db_catch() {
		$expected_mysql_error = 'mysql_error';
		$expected_error = 'error';

		$mock_db_access = $this->getMock('DbAccessor');
		
		$alerter = new Alerter();
		
		try {
			$alerter->db_catch($expected_error, $expected_mysql_error);
		} catch (Exception $ex) {
			$exception_caught = true;
			$this->assertEquals($expected_error, $ex->getMessage());
			$actual = $alerter->get_alerts();
			$this->assertEquals($expected_mysql_error, $actual[0]['message']);
		}
		$this->assertTrue($exception_caught);
	}
}


?>
